﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Manager;
using CrystalDecisions.Web;
using System.Drawing.Printing;
namespace HealthSystem.ReportPage
{
    public partial class Report1 : BasePage
    {
        ReportManager manager = new ReportManager();
        ReportDocument rd = new ReportDocument();
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            string rid = Request["reportid"] as string;
            string tjnd = Request["tjnd"] as string;
            string schooltype = Request["schooltype"] as string;
            string districtid = Request["districtid"] as string;
            string ctype = Request["ctype"].ToString();
            switch (schooltype)
            {
                case "1":
                    schooltype="小学";
                    break;
                case "2":
                    schooltype = "初中";
                    break;
                case "3":
                    schooltype = "高中";
                    break;
                case "4":
                    schooltype = "完中";
                    break;
                default:
                    schooltype = "全部";
                    break;
            }
            string term = (Request["tjnd"] as string) + "年";
            string schoolcntype = (Request["schoolcntype"].ToString());
            ParameterFields paraFields = new ParameterFields();
            ParameterField[] paraField = new ParameterField[4];
            ParameterDiscreteValue[] discreteValue = new ParameterDiscreteValue[4];
            //ParameterRangeValue rangeValue = new ParameterRangeValue();

            paraField[0] = new ParameterField();
            paraField[0].ParameterFieldName = "SchoolStyle";
            discreteValue[0] = new ParameterDiscreteValue();
            discreteValue[0].Value =schooltype;
            paraField[0].CurrentValues.Add(discreteValue[0]);

            paraField[1] = new ParameterField();
            paraField[1].ParameterFieldName = "term";
            discreteValue[1] = new ParameterDiscreteValue();
            discreteValue[1].Value = term;
            paraField[1].CurrentValues.Add(discreteValue[1]);

            paraField[2] = new ParameterField();
            paraField[2].ParameterFieldName = "Unit";
            string workunit = "";
            if (CustomerProfile.AdminUser.hW_Admin_Detail != null)
            {
                workunit = CustomerProfile.AdminUser.hW_Admin_Detail.WorkUnit;
                if (string.IsNullOrEmpty(workunit))
                {
                    workunit = "";
                }
            }
            discreteValue[2] = new ParameterDiscreteValue();
            discreteValue[2].Value = workunit;
            paraField[2].CurrentValues.Add(discreteValue[2]);

            paraField[3] = new ParameterField();
            paraField[3].ParameterFieldName = "Grade";
            discreteValue[3] = new ParameterDiscreteValue();
            discreteValue[3].Value =ctype=="Age"?"年龄":"年级";
            paraField[3].CurrentValues.Add(discreteValue[3]);

            foreach (ParameterField pf in paraField)
            {
                paraFields.Add(pf);
            }
             
            if(schooltype == "全部")
                schooltype = "";
            
                IReport reportmanager = ReportFactory.CreateReport(rid);
                var Report_TB_Grade = reportmanager.GetReport(tjnd, schooltype, districtid, ctype);


                //var Report_TB_Grade = manager.GetReport(rid, tjnd, schooltype,districtid);
                //string path = Server.MapPath("~") + "Reports\\rpt_Jkhzgrade"+rid+".rpt";
                string path = Server.MapPath("~") + "Reports\\"+rid+".rpt";

                rd.Load(path);
                rd.SetDataSource(Report_TB_Grade.Tables[0]);
                CrystalReportViewer1.ReportSource = rd;
                CrystalReportViewer1.ParameterFieldInfo = paraFields;
                CrystalReportViewer1.DataBind();
                CrystalReportViewer1.Visible = true;

                //default printer name
                //PrintDocument prtdoc = new PrintDocument();
                //string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;

                //rd.PrintOptions.PrinterName = strDefaultPrinter;
                //rd.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.DefaultPaperSize;
                //rd.PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Auto;
                //rd.PrintToPrinter(1,true,1,9);
            //}
        }
        protected void Page_UnLoad(object sender, EventArgs e)
        {
            rd.Close();
            rd.Dispose();
        }
    }
}